#include "mapinfotab.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollArea>

MapInfoTab::MapInfoTab(QWidget *parent)
    : QWidget{parent}
    , m_dataTypeCode{0}
    , m_basicInfoFeature{std::make_shared<BasicInfoFeature>()}
    , m_mainWidget{nullptr}
{
    initUI();
}

MapInfoTab::~MapInfoTab()
{
    // 智能指针会自动释放内存
}

uint8_t MapInfoTab::getDataTypeCode() const
{
    return m_dataTypeCode;
}

void MapInfoTab::addFeature(std::shared_ptr<IMapInfoFeature> feature)
{
    if (feature) {
        m_features.push_back(feature);
        
        // 初始化特性UI
        QWidget* featureWidget = feature->initUI(m_mainWidget);
        if (featureWidget) {
            // 将特性UI添加到主布局中
            QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(m_mainWidget->layout());
            if (layout) {
                layout->addWidget(featureWidget);
            }
        }
    }
}

void MapInfoTab::setBasicInfo(const DataSpecificationGW::SpectrumHead& spectrumHead)
{
    m_dataTypeCode = spectrumHead.eSpectrumTypeCode;
    if (m_basicInfoFeature) {
        m_basicInfoFeature->setSpectrumHead(spectrumHead);
    }
}

void MapInfoTab::initUI()
{
    // 创建滚动区域
    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);
    scrollArea->setFrameShape(QFrame::NoFrame);
    
    // 创建主内容区域
    m_mainWidget = new QWidget(scrollArea);
    scrollArea->setWidget(m_mainWidget);
    
    // 设置主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(m_mainWidget);
    m_mainWidget->setLayout(mainLayout);
    
    // 设置主窗口布局
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(scrollArea);
    setLayout(layout);
    
    // 添加基本信息特性
    if (m_basicInfoFeature) {
        QWidget* basicInfoWidget = m_basicInfoFeature->initUI(m_mainWidget);
        if (basicInfoWidget) {
            mainLayout->addWidget(basicInfoWidget);
        }
    }
}

// BasicInfoFeature实现
QWidget* BasicInfoFeature::initUI(QWidget* parent)
{
    QGroupBox* groupBox = new QGroupBox(QObject::tr("基本信息"), parent);
    QVBoxLayout* layout = new QVBoxLayout(groupBox);
    
    m_basicInfoTable = new QTableWidget(groupBox);
    m_basicInfoTable->setColumnCount(2);
    m_basicInfoTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_basicInfoTable->setHorizontalHeaderLabels(QStringList() << QObject::tr("项目") << QObject::tr("值"));
    m_basicInfoTable->horizontalHeader()->setStretchLastSection(true);
    m_basicInfoTable->verticalHeader()->setVisible(false);
    
    layout->addWidget(m_basicInfoTable);
    groupBox->setLayout(layout);
    
    return groupBox;
}

void BasicInfoFeature::loadFromSpectrum(DataSpecificationGW::Spectrum* spectrum)
{
    if (!spectrum) return;
    
    DataSpecificationGW::SpectrumHead head;
    spectrum->getSpectrumHead(head);
    setSpectrumHead(head);
}

void BasicInfoFeature::setSpectrumHead(const DataSpecificationGW::SpectrumHead& head)
{
    if (!m_basicInfoTable) return;
    
    m_basicInfoTable->setRowCount(0);
    
    // 添加图谱基本信息
    int row = 0;
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("检测数据类型编码")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(QString::number(head.eSpectrumTypeCode)));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("图谱数据长度")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(QString::number(head.iSpectrumDataLength)));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("图谱生成时间")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(head.qstrSpectrumGenerationTime));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("被测设备名称")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(head.qstrEquipmentName));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("被测设备编码")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(head.qstrEquipmentCode));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("测点名称")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(head.qstrTestPointName));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("测点编码")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(head.qstrTestPointCode));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("检测通道标志")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(QString::number(head.sDetectionChannelID)));
    
    m_basicInfoTable->insertRow(row);
    m_basicInfoTable->setItem(row, 0, createTableWidgetItem(QObject::tr("存储数据类型")));
    m_basicInfoTable->setItem(row++, 1, createTableWidgetItem(QString::number(head.eStorageDataType)));
    
    // 自动调整行高
    m_basicInfoTable->resizeRowsToContents();
}

QTableWidgetItem* BasicInfoFeature::createTableWidgetItem(const QString& text)
{
    QTableWidgetItem* item = new QTableWidgetItem(text);
    item->setTextAlignment(Qt::AlignCenter);
    return item;
}

void MapInfoTab::setDataTypeCode(uint8_t code)
{
    Q_UNUSED(code);
}

void MapInfoTab::setDataLength(int32_t length)
{
    Q_UNUSED(length);
}

void MapInfoTab::setCreateTime(const QString& time)
{
    Q_UNUSED(time);
}

void MapInfoTab::setChartProperty(const DataSpecificationGW::SpectrumCharacter& property)
{
    Q_UNUSED(property);
}

void MapInfoTab::setDeviceName(const QString &name)
{
    Q_UNUSED(name);
}

void MapInfoTab::setDeviceCode(const QString &code)
{
    Q_UNUSED(code);
}

void MapInfoTab::setMeasurementName(const QString &name)
{
    Q_UNUSED(name);
}

void MapInfoTab::setMeasurementCode(const QString &code)
{
    Q_UNUSED(code);
}

void MapInfoTab::setChannelFlag(int64_t flag)
{
    Q_UNUSED(flag);
}

void MapInfoTab::setDataType(uint8_t type)
{
    Q_UNUSED(type);
}

void MapInfoTab::setAmplitudeUnit(uint8_t unit)
{
    Q_UNUSED(unit);
}

void MapInfoTab::setAmplitudeLowerLimit(float lowerLimit)
{
    Q_UNUSED(lowerLimit);
}

void MapInfoTab::setAmplitudeUpperLimit(float upperLimit)
{
    Q_UNUSED(upperLimit);
}

void MapInfoTab::setFrequencyBand(uint8_t band)
{
    Q_UNUSED(band);
}

void MapInfoTab::setLowerFrequency(float lowerFreq)
{
    Q_UNUSED(lowerFreq);
}

void MapInfoTab::setUpperFrequency(float upperFreq)
{
    Q_UNUSED(upperFreq);
}

void MapInfoTab::setPhaseWindowCount(int count)
{
    Q_UNUSED(count);
}

void MapInfoTab::setQuantizedAmplitude(int32_t n)
{
    Q_UNUSED(n);
}

void MapInfoTab::setDataPointCount(int count)
{
    Q_UNUSED(count);
}

void MapInfoTab::setFrequencyPeriod(int32_t num)
{
    Q_UNUSED(num);
}

void MapInfoTab::setPRPSData(const QByteArray &byte)
{
    Q_UNUSED(byte);
}

void MapInfoTab::setPRPDData(const QByteArray &byte)
{
    Q_UNUSED(byte);
}

void MapInfoTab::setSamplingRate(int64_t rate)
{
    Q_UNUSED(rate);
}

void MapInfoTab::setTimeDomainWaveformData(const QString &data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setDischargeTypeProbability(const QString &probability)
{
    Q_UNUSED(probability);
}

void MapInfoTab::setUltrasonicSensorType(float type)
{
    Q_UNUSED(type);
}

void MapInfoTab::setPulseIntervalTimeUnit(uint8_t unit)
{
    Q_UNUSED(unit);
}

void MapInfoTab::setAEPhaseChartData(const QByteArray &data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setAEPulseChartData(const QByteArray &data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setAEWaveformData(const QByteArray &data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setTestFrequency(float frequency)
{
    Q_UNUSED(frequency);
}

void MapInfoTab::setSignalMaxValue(float value)
{
    Q_UNUSED(value);
}

void MapInfoTab::setSignalRMSValue(float value)
{
    Q_UNUSED(value);
}

void MapInfoTab::setCorrelation50Hz(float correlation)
{
    Q_UNUSED(correlation);
}

void MapInfoTab::setCorrelation100Hz(float correlation)
{
    Q_UNUSED(correlation);
}

void MapInfoTab::setTransientVoltageAmplitudeData(const QString &data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setMaxTransientVoltageAmplitude(float amplitude)
{
    Q_UNUSED(amplitude);
}

void MapInfoTab::setPulseCount(int32_t count)
{
    Q_UNUSED(count);
}

void MapInfoTab::setTemperatureUnit(uint8_t unit)
{
    Q_UNUSED(unit);
}

void MapInfoTab::setTemperatureMatrixWidth(int32_t width)
{
    Q_UNUSED(width);
}

void MapInfoTab::setTemperatureMatrixHeight(int32_t height)
{
    Q_UNUSED(height);
}

void MapInfoTab::setVisibleLightPhotoDataLength(int32_t length)
{
    Q_UNUSED(length);
}

void MapInfoTab::setInfraredPhotoDataLength(int32_t length)
{
    Q_UNUSED(length);
}

void MapInfoTab::setEmissivity(float emissivity)
{
    Q_UNUSED(emissivity);
}

void MapInfoTab::setTestDistance(float distance)
{
    Q_UNUSED(distance);
}

void MapInfoTab::setAtmosphericTemperature(float temperature)
{
    Q_UNUSED(temperature);
}

void MapInfoTab::setRelativeHumidity(int8_t humidity)
{
    Q_UNUSED(humidity);
}

void MapInfoTab::setReflectionTemperature(float temperature)
{
    Q_UNUSED(temperature);
}

void MapInfoTab::setTemperatureUpperLimit(float upperLimit)
{
    Q_UNUSED(upperLimit);
}

void MapInfoTab::setTemperatureLowerLimit(float lowerLimit)
{
    Q_UNUSED(lowerLimit);
}

void MapInfoTab::setInfraredChartData(const QByteArray &data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setVisibleLightPhotoData(uint8_t data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setInfraredPhotoData(uint8_t data)
{
    Q_UNUSED(data);
}

void MapInfoTab::setBackgroundFileName(const QString &fileName)
{
    Q_UNUSED(fileName);
}

void MapInfoTab::setReservedData(const QByteArray &byte)
{
    Q_UNUSED(byte);
}
